#!/usr/bin/env python
# -*- coding:utf-8 -*-

# https://www.cnblogs.com/yang-2018/p/11824250.html
# TreeView组件是一个树状结构和表格的结合体。第一列是树状结构，后几列是列表。
# 每一行表示一个item，树的item可以分级，每个item有子item，名称对应text标签。每一行的值用values元组里的值表示。
# item有5个标签，分别是：text，image，values，open，tags。

import tkinter as tk
from tkinter import ttk


# 表格代码
def test_1():
    window = tk.Tk()
    # 设置窗口大小
    winWidth = 600
    winHeight = 400
    # 获取屏幕分辨率
    screenWidth = window.winfo_screenwidth()
    screenHeight = window.winfo_screenheight()

    x = int((screenWidth - winWidth) / 2)
    y = int((screenHeight - winHeight) / 2)

    # 设置主窗口标题
    window.title("TreeView参数说明")
    # 设置窗口初始位置在屏幕居中
    window.geometry("%sx%s+%s+%s" % (winWidth, winHeight, x, y))
    # 设置窗口图标
    # window.iconbitmap("./image/icon.ico")
    # 设置窗口宽高固定
    window.resizable(0, 0)

    # 定义列的名称
    columns = ("name", "gender", "age")
    tree = ttk.Treeview(window, show="headings", columns=columns, selectmode=tk.BROWSE)

    # 设置表格文字居中
    tree.column("name", anchor="center")
    tree.column("gender", anchor="center")
    tree.column("age", anchor="center")

    # 设置表格头部标题
    tree.heading("name", text="姓名")
    tree.heading("gender", text="性别")
    tree.heading("age", text="年龄")

    # 设置表格内容
    lists = [{"name": "yang", "gender": "男", "age": "18"}, {"name": "郑", "gender": "女", "age": "25"}]
    i = 0
    for v in lists:
        tree.insert('', i, values=(v.get("name"), v.get("gender"), v.get("age")))
        i += 1

    tree.pack(expand=True, fill=tk.BOTH)

    # 获取当前点击行的值
    def treeviewClick(event):  # 单击
        for item in tree.selection():
            item_text = tree.item(item, "values")
            print(item_text)

    # 鼠标左键抬起
    tree.bind('<ButtonRelease-1>', treeviewClick)

    # 鼠标选中一行回调
    def selectTree(event):
        for item in tree.selection():
            item_text = tree.item(item, "values")
            print(item_text)

    # 选中行
    # tree.bind('<<TreeviewSelect>>', selectTree)

    window.mainloop()


# 树状代码
def test_2():
    window = tk.Tk()
    # 设置窗口大小
    winWidth = 600
    winHeight = 400
    # 获取屏幕分辨率
    screenWidth = window.winfo_screenwidth()
    screenHeight = window.winfo_screenheight()

    x = int((screenWidth - winWidth) / 2)
    y = int((screenHeight - winHeight) / 2)

    # 设置主窗口标题
    window.title("TreeView参数说明")
    # 设置窗口初始位置在屏幕居中
    window.geometry("%sx%s+%s+%s" % (winWidth, winHeight, x, y))
    # 设置窗口图标
    # window.iconbitmap("./image/icon.ico")
    # 设置窗口宽高固定
    window.resizable(0, 0)

    # 定义列的名称
    tree = ttk.Treeview(window, show="tree")

    myid = tree.insert("", 0, "中国", text="中国China", values=("1"))  # ""表示父节点是根
    myidx1 = tree.insert(myid, 0, "广东", text="中国广东", values=("2"))  # text表示显示出的文本，values是隐藏的值
    myidx2 = tree.insert(myid, 1, "江苏", text="中国江苏", values=("3"))
    myidy = tree.insert("", 1, "美国", text="美国USA", values=("4"))
    myidy1 = tree.insert(myidy, 0, "加州", text="美国加州", values=("5"))

    # 鼠标选中一行回调
    def selectTree(event):
        for item in tree.selection():
            item_text = tree.item(item, "values")
            print(item_text)

    # 选中行
    tree.bind('<<TreeviewSelect>>', selectTree)

    tree.pack(expand=True, fill=tk.BOTH)

    window.mainloop()


def main():
    from public_function import dict_choice
    choice = str(input("Enter a positive integer to choice: "))
    if choice in dict_choice.keys():
        exec(dict_choice[choice])
    else:
        print("输入有误，请重输！", choice)


if __name__ == "__main__":
    main()
